
function Load-PowerShellObjectTree
{
	[void] [Reflection.Assembly]::LoadFile( `
		(get-childitem "$_CommonFunctionsDir\Libs\PowerShellObjectTree.dll"))
	
	$__IterateObjectTree = new-object "PowerShellObjectTree.IterateObjectTree"
}

Load-PowerShellObjectTree

# ===================================================================================
function Get-ObjectTreeReader([switch]$NoExpansion)
{
  if ($NoExpansion)
  {
    $reader = new-object "PowerShellObjectTree.ObjectTreeReader"
  }
  else
  {
    $reader = new-object "PowerShellObjectTree.ObjectTreeReader" $ExecutionContext
  }
  $reader
}
# ===================================================================================
function ReadXmlFileIntoObjectTree
{
  param ([string]$filePath, [switch]$NoExpansion)

  (Get-ObjectTreeReader -NoExpansion:$NoExpansion).readfile($filePath)
}

# ===================================================================================
function ReadXmlIntoObjectTree
{
  param ([xml]$xml, [switch]$NoExpansion)

  (Get-ObjectTreeReader -NoExpansion:$NoExpansion).ReadXml($xml)
}

# ===================================================================================
function IterateObjectTree
{
  param ( $psObj, [string]$path, [scriptblock]$script )
  
  [scriptblock]$_scriptWrapper = { 
    $_ = $Args[0]
    &$script $_
  }
  $__IterateObjectTree.Execute($psObj,$path,$_scriptWrapper)
}

# ===================================================================================

function Apply-PSClassToChildren
{
  param ( $PSClass, $psObj, [string]$path )
  
  [scriptblock]$_scriptWrapper = { 
    $_ = $Args[0]

    [void] $PSClass.AttachTo($_)
  }
  
  if ([string]::IsNullOrEmpty($path))
  {
    &$_scriptWrapper $psObj > $null
  }
  else
  {
    $__IterateObjectTree.Execute($psObj,$path,$_scriptWrapper)
  }
}
